//
//  RCResultSet.m
//  RCDataBaseProject
//
//  Created by Roiy on 2017/7/31.
//  Copyright © 2017年 Roiy. All rights reserved.
//

#import "RCResultSet.h"
#import "RCDataBaseManager.h"
#import <sqlite3.h>


@interface RCResultSet()
{
    NSMutableDictionary *_columnNameForIndexDic;
}
@end

@implementation RCResultSet

+(instancetype)resultSetWithStatement:(RCStatement *)statement parentDB:(RCSQLDataBaseManager *)aDB
{
    RCResultSet *rcResultSet = [[RCResultSet alloc]init];
    [rcResultSet setParentDB:aDB];
    [rcResultSet setStatement:statement];
    return rcResultSet;
}

#pragma mark ---Sqlite Handle--
-(BOOL)nextRefer
{
    int handleResult = sqlite3_step([_statement statement]);
    if (handleResult == SQLITE_BUSY || handleResult ==SQLITE_LOCKED) {
        NSLog(@"当前句柄正在访问"); //busy处理
    }
    if (handleResult == SQLITE_MISUSE){
        NSLog(@"当前句柄位置错误");//misuse处理
    }
    if (handleResult == SQLITE_ERROR){
        NSLog(@"当前句柄产生错误");
    }
    return  (handleResult == SQLITE_ROW);
}

-(NSString *)stringForColumnIndex:(int)columnIndex {
    if (sqlite3_column_type([_statement statement], columnIndex) == SQLITE_NULL || (columnIndex < 0) || columnIndex >= sqlite3_column_count([_statement statement])) {
        NSLog(@"ERROR：查询列不在数据表中");
        return nil;
    }
    const char *resultString = (const char *)sqlite3_column_text([_statement statement], columnIndex);
    if (!resultString) {
        return nil;
    }
    return [NSString stringWithUTF8String:resultString];
}

-(NSData *)dataForColumnIndex:(int)columnIndex{
    if (sqlite3_column_type([_statement statement], columnIndex) == SQLITE_NULL || (columnIndex <0) || columnIndex >= sqlite3_column_count([_statement statement])) {
        NSLog(@"ERROR：查询列不在数据表中");
        return nil;
    }
    const char *dataBuffer = sqlite3_column_blob([_statement statement], columnIndex);
    int dataSize = sqlite3_column_bytes([_statement statement], columnIndex);
    if (dataBuffer == NULL)
    {
        return nil;
    }
    return [NSData dataWithBytes:dataBuffer length:(NSInteger)dataSize];
}

-(int)intForColumnIndex:(int)columnIndex
{
    if (sqlite3_column_type([_statement statement], columnIndex) == SQLITE_NULL || (columnIndex <0) || columnIndex >= sqlite3_column_count([_statement statement])) {
        NSLog(@"ERROR：查询列不在数据表中");
    }
    return sqlite3_column_int([_statement statement], columnIndex);
}

-(long)longForColumnIndex:(int)columnIndex
{
    if (sqlite3_column_type([_statement statement], columnIndex) == SQLITE_NULL || (columnIndex <0) || columnIndex >= sqlite3_column_count([_statement statement])) {
        NSLog(@"ERROR：查询列不在数据表中");
    }
    return sqlite3_column_int64([_statement statement], columnIndex);
}

-(BOOL)boolForColumnIndex:(int)columnIndex
{
    return ([self intForColumnIndex:columnIndex] != 0);
}

-(double)doubleForColumnIndex:(int)columnIndex
{
    if (sqlite3_column_type([_statement statement], columnIndex) == SQLITE_NULL || (columnIndex <0) || columnIndex >= sqlite3_column_count([_statement statement])) {
        NSLog(@"ERROR：查询列不在数据表中");
    }
    return sqlite3_column_double([_statement statement], columnIndex);
}

#pragma mark ---Query Type--
-(NSString*)stringForColumn:(NSString*)columnName
{
    return [self stringForColumnIndex:[[[self columnNameToIndexMap] objectForKey:columnName] intValue]];
}

-(NSData *)dataForColumn:(NSString *)columnName
{
    return [self dataForColumnIndex:[[[self columnNameToIndexMap] objectForKey:columnName] intValue]];
}

-(int)intForColumn:(NSString *)columnName
{
    return [self intForColumnIndex:[[[self columnNameToIndexMap] objectForKey:columnName] intValue]];
}

-(long)longForColumn:(NSString *)columnName
{
    return [self longForColumnIndex:[[[self columnNameToIndexMap] objectForKey:columnName] intValue]];
}

-(BOOL)boolForColumn:(NSString*)columnName
{
    return  [self boolForColumnIndex:[[[self columnNameToIndexMap] objectForKey:columnName] boolValue]];
}

-(double)doubleForColumn:(NSString *)columnName
{
    return [self doubleForColumnIndex:[[[self columnNameToIndexMap] objectForKey:columnName] doubleValue]];
}


#pragma mark ---Public Method--
- (NSMutableDictionary *)columnNameToIndexMap {
    if (!_columnNameForIndexDic) {
        int columnCount = sqlite3_column_count([_statement statement]);
        _columnNameForIndexDic = [[NSMutableDictionary alloc] initWithCapacity:(NSUInteger)columnCount];
        int columnIdx = 0;
        for (columnIdx = 0; columnIdx < columnCount; columnIdx++) {
            [_columnNameForIndexDic setObject:[NSNumber numberWithInt:columnIdx]
                                      forKey:[[NSString stringWithUTF8String:sqlite3_column_name([_statement statement], columnIdx)] lowercaseString]];
        }
    }
    return _columnNameForIndexDic;
}

@end
